package com.qsbc.pygram.controller;


import com.qsbc.pygram.common.Result;
import com.qsbc.pygram.entity.Teacher;
import com.qsbc.pygram.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-11-14
 */
@RestController
@RequestMapping("/pygram/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping
    public List<Teacher> findAll(){
        return teacherService.list();
    }

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Teacher teacher) {
        if (teacher.getId() == null && teacher.getPassword() == null) {  // 新增用户默认密码
            teacher.setPassword("123456");
        }
        return Result.success(teacherService.saveOrUpdate(teacher));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(teacherService.removeById(id));
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(teacherService.removeByIds(ids));
    }
//    // 分页查询 - mybatis-plus的方式
//    @GetMapping("/page")
//    public Result findPage(@RequestParam Integer pageNum,
//                           @RequestParam Integer pageSize,
//                           @RequestParam(defaultValue ="") String username) {
//
//        return Result.success(teacherService.findPage(new Page<>(pageNum, pageSize), username));
//    }
}
